//
//  HMSearchViewController.m
//  HMSupplier
//
//  Created by haohao on 2017/7/7.
//  Copyright © 2017年 haohao. All rights reserved.
//

#import "HMSearchViewController.h"
#import "HistoryRecond.h"
#import "OrderRecondTableViewCell.h"
#import "HMSearchTypeTableViewCell.h"
#import "HMSearchResultViewController.h"
#import "PeopleCenter.h"
#import "PeopleCenterToolkit.h"
#import "SearchModel.h"

#import "TimeAlertView.h"

@interface HMSearchViewController ()<UITableViewDelegate,UITableViewDataSource, HistoryRecondDelegate, UITextFieldDelegate, TimeAlertViewDelegate>

NS_STRONG UITableView *myTableView;

NS_STRONG NSMutableArray *recondArray;

NS_STRONG UITextField *searchText;

NS_STRONG HistoryRecond *historyRecond;

NS_STRONG NSArray *dataArray;
@end

@implementation HMSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpSubView];
}

- (void)setUpSubView {
    self.view.backgroundColor = ke5e5e5;
    
    _recondArray = [[NSMutableArray alloc] initWithArray:[MGAccountTool getRecond]];
    
    _searchText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, KWidthSCREEN - 150, 30)];
    _searchText.delegate = self;
    _searchText.layer.cornerRadius = 5;
    _searchText.clipsToBounds = YES;
    _searchText.backgroundColor = [UIColor whiteColor];
    _searchText.placeholder = @"请输入菜名";
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    
    UIImageView *leftImageVI = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
    leftImageVI.image = [UIImage imageNamed:@"people_screen_btn_search"];
    [leftView addSubview:leftImageVI];
    
    _searchText.leftView = leftView;
    _searchText.leftViewMode=UITextFieldViewModeAlways;
    self.navigationItem.titleView = _searchText;

    
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(0, 0, 40, 40);
    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
    
    
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWidthSCREEN, KHeightSCREEN) style:UITableViewStyleGrouped];
    _myTableView.backgroundColor = ke5e5e5;
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [self.view addSubview:_myTableView];
    
    
    
    _historyRecond = [[HistoryRecond alloc] init];
    _historyRecond.delegate = self;

    if (_recondArray.count != 0) {
        _myTableView.tableHeaderView = _historyRecond;
    }
}

- (void)search {
    [self.view endEditing:YES];
    
    _recondArray = [PeopleCenterToolkit addSearchText:_recondArray searchText:_searchText.text];
    
    [MGAccountTool saveRecond:_recondArray];
    [_historyRecond refreshData];
    
    [_myTableView beginUpdates];
    [_myTableView setTableHeaderView:_historyRecond];
    [_myTableView endUpdates];
    
    [self getData:_searchText.text time:@""];
}

- (void)getData:(NSString *)searchResult time:(NSString *)time{
    if (searchResult.length == 0) {
        [HMHUDManager showHUDWithText:@"请输入搜索内容"];
    } else {
        [PeopleCenter searchWith:searchResult time:time success:^(NSArray *dataArray) {
            HMSearchResultViewController *vc = [[HMSearchResultViewController alloc] init];
            vc._title = searchResult;
            vc.dataArray = dataArray;
            [self.navigationController pushViewController:vc animated:YES];
        } failure:^(NSError *error) {
            
        }];
    }
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HMSearchTypeTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"HMSearchTypeTableViewCell" owner:self options:nil] firstObject];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TimeAlertView *timeAlert = [[TimeAlertView alloc] init];
    timeAlert.delegate = self;
    [timeAlert show];
}

#pragma mark - TimeAlertViewDelegate
- (void)pickerView:(TimeAlertView *)alertView clickedButtonWithDate:(NSString *)date {
    [PeopleCenter searchWith:@"" time:date success:^(NSArray *dataArray) {
        HMSearchResultViewController *vc = [[HMSearchResultViewController alloc] init];
        vc.dataArray = dataArray;
        vc._title = @"获取结果";
        [self.navigationController pushViewController:vc animated:YES];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - HistoryRecondDelegate
- (void)returnSearchString:(NSString *)searchString {
    [self getData:searchString time:@""];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    //if (string.length == 0 && range.location == 0 && range.length == 1) {
    //  _myTableView.tableHeaderView = _historyRecond;
    //} else {
    //    _myTableView.tableHeaderView = nil;

    //}
    
    return YES;
}


@end
